import AppNavigator from '../navigators'

const recentlyVisitedRoutes = new Set()

function nav (state, action) {
  let nextState = AppNavigator.router.getStateForAction(action, state)
  if (action.type === 'Navigation/NAVIGATE' || action.type === 'Navigation/BACK') {
    if (recentlyVisitedRoutes.has(action.routeName)) {
      return state
    }
    recentlyVisitedRoutes.add(action.routeName)
    setTimeout(() => { recentlyVisitedRoutes.delete(action.routeName) }, 1000)
  }
  return nextState || state
}


// reducer的函数签名: reducer(previousState, action)
function list(state={data: []}, action) {
  switch (action.type) {
    case 'SET_LIST':
      return {
        data: action.data
      }
    default: 
      return state
  }
}

const AppReducers = {
  nav,
  list
}

export default AppReducers
